﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "GnufModelViewBuildingStoreyVisibilityFilter.h"
#include "GnufModelViewDefine.h"
#include "GnufModelViewElementVisibilityFilter.h"
#include "GnufModelViewVisibilityFilterNames.h"
#include "GnufModelViewVisibilityFilterUtils.h"
#include "GnufModelViewWithinRangeVisibilityFilter.h"
#include "IDocument.h"
#include "IElementVisibilityFilter.h"
#include "IElementVisibilityFilterManager.h"
#include "IModelView.h"
#include "IUiDocument.h"
#include "IUiView.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

EXPOSE_DEBUG_MODE_EXPORT(GnufModelViewVisibilityFilterUtils, COMPONENT_EDITOR_GNUF_UI_EXPORT);

using namespace gcmp;
using namespace gfam;

namespace
{
    // 该视图是否支持按视图范围显示
    bool IsModelViewRangeVisibilityFilterSupported(const IModelView* pModelView)
    {
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelViwe为空", L"paramodel", L"2024-02-20");

        return true;
    }

    // 该视图是否支持按楼层显示
    bool IsBuildingStoreyVisibilityFilterSupported(const IModelView* pModelView)
    {
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelViwe为空", L"paramodel", L"2024-02-20");
        const std::wstring& viewType = L"View3DViewType";
        //const std::wstring& viewType = pModelView->GetUserDataId();
        if (viewType == PlanViewType ||
            viewType == View3DViewType)
        {
            return true;
        }
        return false;
    }
}

//////////////////////////////////////////////////////////////////////////
std::vector<std::wstring> GnufModelViewVisibilityFilterUtils::GetModelViewSupportedFilters(const IModelView* pModelView)
{
    std::vector<std::wstring> internalNames;
    DBG_WARN_AND_RETURN_UNLESS(pModelView, internalNames, L"pModelViwe为空", L"paramodel", L"2024-02-20");

    if (IsModelViewRangeVisibilityFilterSupported(pModelView))
    {
        internalNames.push_back(GNUF_MODEL_VIEW_VISIBILITY_FILTER_WITHIN_RANGE);
    }

    if (IsBuildingStoreyVisibilityFilterSupported(pModelView))
    {
        internalNames.push_back(GNUF_MODEL_VIEW_VISIBILITY_FILTER_BUILDING_STOREY);
    }

    return internalNames;
}

gcmp::OwnerPtr<gcmp::IElementVisibilityFilter> gfam::GnufModelViewVisibilityFilterUtils::CreateDocumentFilter(const std::wstring & filterName)
{
    OwnerPtr<IElementVisibilityFilter> opFilter;
    if (filterName == GNUF_MODEL_VIEW_VISIBILITY_FILTER_WITHIN_RANGE)
    {
        opFilter = NEW_AS_OWNER_PTR(ModelViewWithinRangeVisibilityFilter);
    }
    else
    {
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(false, L"未知的文档显示过滤器名，调用前请先实现过滤器！", L"paramodel", L"2024-02-20");
    }

    return TransferOwnership(opFilter);
}

bool gfam::GnufModelViewVisibilityFilterUtils::AddVisibilityFilter(gcmp::IDocument * pDoc, const std::wstring & filterName)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilterManager* pVisibilityFiletrMgr = pDoc->GetElementVisibilityFilterManager();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pVisibilityFiletrMgr, L"pVisibilityFiletrMgr为空", L"paramodel", L"2024-02-20");

    OwnerPtr<IElementVisibilityFilter> opFilter;
    if (!pVisibilityFiletrMgr->GetElementVisibilityFilter(filterName))
    {
        opFilter = CreateDocumentFilter(filterName);
    }
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opFilter, L"opFilter为空", L"paramodel", L"2024-02-20");

    bool bOk = pVisibilityFiletrMgr->AddElementVisibilityFilter(TransferOwnership(opFilter));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"AddElementVisibilityFilter失败", L"paramodel", L"2024-02-20");

    return true;
}

std::wstring GnufModelViewVisibilityFilterUtils::GetModelViewDefaultFilter(const IModelView* pModelView)
{
    DBG_WARN_AND_RETURN_UNLESS(pModelView, L"", L"pModelViwe为空", L"paramodel", L"2024-02-20");
    if (IsModelViewRangeVisibilityFilterSupported(pModelView))
    {
        return GNUF_MODEL_VIEW_VISIBILITY_FILTER_WITHIN_RANGE;
    }
    return GNUF_MODEL_VIEW_VISIBILITY_FILTER_BUILDING_STOREY;
}

OwnerPtr<IElementVisibilityFilter> GnufModelViewVisibilityFilterUtils::CreateModelViewFilter(const std::wstring& filterName)
{
    if (filterName.compare(GNUF_MODEL_VIEW_VISIBILITY_FILTER_WITHIN_RANGE) == 0)
    {
        return NEW_AS_OWNER_PTR(ModelViewWithinRangeVisibilityFilter);
    }
    else if (filterName.compare(GNUF_MODEL_VIEW_VISIBILITY_FILTER_BUILDING_STOREY) == 0)
    {
        return NEW_AS_OWNER_PTR(ModelViewBuildingStoreyVisibilityFilter);
    }
    else
    {
        DBG_WARN(L"ruleInternalName无法识别", L"paramodel", L"2024-02-20");
    }

    return nullptr;
}

bool GnufModelViewVisibilityFilterUtils::ActiveModelViewFilter(IModelView* pModelView, const std::wstring& filterName, bool deactiveOthers)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelViwe为空", L"paramodel", L"2024-02-20");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(!filterName.empty(), L"filterName为空", L"paramodel", L"2024-02-20");

    const auto& ruleNames = GetModelViewSupportedFilters(pModelView);
    if (ruleNames.cend() == std::find(ruleNames.cbegin(), ruleNames.cend(), filterName))
        return false;

    IElementVisibilityFilter* pVisibilityFilter = pModelView->GetElementVisibilityFilterFw(filterName);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pVisibilityFilter, L"pVisibilityFilter为空", L"paramodel", L"2024-02-20");

    ModelViewElementVisibilityFilter *pModelViewVisibilityFilter = quick_cast<ModelViewElementVisibilityFilter>(pVisibilityFilter);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelViewVisibilityFilter, L"pModelViewVisibilityFilter为空", L"paramodel", L"2024-02-20");
    bool bOk = pModelViewVisibilityFilter->OnActivated(pModelView);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"OnActivated失败", L"paramodel", L"2024-02-20");

    if (deactiveOthers)
    {
        // 禁用所有非当前的显示过滤规则
        for (const auto& ruleName : ruleNames)
        {
            if (ruleName == filterName)
                continue;

            std::wstring deactiveVisibilityFilterName = ruleName;

            IElementVisibilityFilter* pDeactiveFilter = pModelView->GetElementVisibilityFilterFw(deactiveVisibilityFilterName);
            DBG_WARN_AND_CONTINUE_UNLESS(pDeactiveFilter, L"pDeactiveFilter为空", L"paramodel", L"2024-02-20");

            ModelViewElementVisibilityFilter *pModelViewDeactiveFilter = quick_cast<ModelViewElementVisibilityFilter>(pDeactiveFilter);
            DBG_WARN_AND_CONTINUE_UNLESS(pModelViewDeactiveFilter, L"pModelViewDeactiveFilter为空", L"paramodel", L"2024-02-20");
            bOk = pModelViewDeactiveFilter->OnDeactived(pModelView);
            DBG_WARN_AND_CONTINUE_UNLESS(bOk, L"OnDeactived失败", L"paramodel", L"2024-02-20");
        }
    }

    return true;
}